<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>栈</title>
</head>

<body>
    <script>
        function Stack() {
            // dataStore用来当作栈的容器
            this.dataStore = [];
            //top表示栈顶指针
            this.top = 0;
            // push函数压栈
            this.push = push;
            // pop函数出栈
            this.pop = pop;
            // peek函数返回栈顶元素，但是不会弹出
            this.peek = peek;
            // clear函数清空当前栈
            this.clear = clear;
            // length表示当前栈中的元素
            this.length = length;
        }
        // 压栈
        function push(ele) {
            this.dataStore[this.top++] = element;
        }
        // 返回栈顶元素
        function peek() {
            return dataStore[this.top - 1];
        }
        // 出栈
        function pop(ele) {
            return this.dataStore[--this.top];
        }
        // 将当前栈顶指针置为0
        function clear() {
            this.top = 0;
        }
        // 返回栈中元素的个数
        function length() {
            return this.top;
        }
    </script>
</body>

</html>